import sys
from PyQt4 import QtGui, QtCore
from box import *
from window import *
from questionMark_solver import *
from enum_classes import *
import DSLParser


clr = [[255,0,0],[0,0,255]]
clrField = 0
paintRight = 0


def rd_Togg(caller , event_name):
   global clrField
   if( event_name == "rd_toglled[Red]" ):
      if (clrField == 0):
         if (main_dict["main_window"].get_Gui_Var("rd_val[Red]") == False):
            main_dict["main_window"].change_var("rd_val[Red]",True)
         return
      else:
         if (main_dict["main_window"].get_Gui_Var("rd_val[Red]") == False):
            return
         else:
            clrField = 0
            main_dict["main_window"].change_var("rd_val[Green]",False)
            main_dict["main_window"].change_var("rd_val[Blue]",False)
            repaint()
   if( event_name == "rd_toglled[Green]" ):
      if (clrField == 1):
         if (main_dict["main_window"].get_Gui_Var("rd_val[Green]") == False):
            main_dict["main_window"].change_var("rd_val[Green]",True)
         return
      else:
         if (main_dict["main_window"].get_Gui_Var("rd_val[Green]") == False):
            return
         else:
            clrField = 1
            main_dict["main_window"].change_var("rd_val[Red]",False)
            main_dict["main_window"].change_var("rd_val[Blue]",False)
            repaint()
   if( event_name == "rd_toglled[Blue]" ):
      if (clrField == 2):
         if (main_dict["main_window"].get_Gui_Var("rd_val[Blue]") == False):
            main_dict["main_window"].change_var("rd_val[Blue]",True)
         return
      else:
         if (main_dict["main_window"].get_Gui_Var("rd_val[Blue]") == False):
            return
         else:
            clrField = 2
            main_dict["main_window"].change_var("rd_val[Red]",False)
            main_dict["main_window"].change_var("rd_val[Green]",False)
            repaint()



def slide(caller, event_name = None):
   val = main_dict["main_window"].get_Gui_Var("slider_val")
   clr[paintRight][clrField] = val
   repaint()
   
def whenChecked(caller, event_name = None):
   global paintRight
   if ( main_dict["main_window"].get_Gui_Var("check_val") ):
      paintRight = 1
   else:
      paintRight = 0
   repaint()

def repaint():
   main_dict["main_window"].change_var("slider_val",clr[paintRight][clrField])
   main_dict["main_window"].change_var("color1",getColFromIntList(clr[0]))
   main_dict["main_window"].change_var("color2",getColFromIntList(clr[1]))
      
def getColFromIntList(lst):
   r = str(hex(lst[0])[2:] )
   if (len(r) < 2):
      r = "0"+r
   g = str(hex(lst[1])[2:] )
   if (len(g) < 2):
      g = "0"+g
   b = str(hex(lst[2])[2:] )
   if (len(b) < 2):
      b = "0"+b
   return r + g + b

def change_labels_text(caller, event_name = None):
   global paintRight
   cap = main_dict["main_window"].get_Gui_Var("combo_var")
   if paintRight==0:
      if cap==0:
         main_dict["main_window"].change_var("lab1_text","Label 1")
      else:
         main_dict["main_window"].change_var("lab1_text","Label 2")
   else:
      if cap==0:
         main_dict["main_window"].change_var("lab2_text","Label 1")
      else:
         main_dict["main_window"].change_var("lab2_text","Label 2")

   

if __name__ == '__main__':
   app = QtGui.QApplication(sys.argv)
   filename = "all_widget_showDown.txt"
   parsed_file = DSLParser.parseFile(filename)
   box_dict = parsed_file[0]
   main_dict = solve(box_dict)
   main_dict["main_window"].bind("rd_toglled[Red]",rd_Togg)
   main_dict["main_window"].bind("rd_toglled[Green]",rd_Togg)
   main_dict["main_window"].bind("rd_toglled[Blue]",rd_Togg)
   main_dict["main_window"].bind("slide",slide)
   main_dict["main_window"].bind("checked",whenChecked)
   main_dict["main_window"].bind("combo_changed",change_labels_text)
   main_dict["main_window"]({}) 
   sys.exit(app.exec_())
 






























